﻿public class AbstractMassageFactory {
    public static int MessageCnt = 0;
    public List<Music> MusicDatabase = new List<Music>() {
        new Music(0, 1, "Song A", "Artist A", "Label A"),
        new Music(0, 2, "Song B", "Artist B", "Label B")
    };
    virtual public IChatMessage? GetMessage(string data) {
        Console.WriteLine("error");
        return null;
    }
}
public class ContentFactory : AbstractMassageFactory{
    override public Content GetMessage(string data) {
        return new Content(++MessageCnt,data);
    }
}
public class PictureFactory : AbstractMassageFactory{
    override public Picture GetMessage(string data) {
        return new Picture(++MessageCnt,data);
    }
}
public class MusicFactory : AbstractMassageFactory{
    override public Music? GetMessage(string data) {
        Music? res = MusicDatabase
            .Where(m => m.Name == data) // 查找Name属性等于data的Music对象
            .Select(m => m)  // 使用复制构造函数创建找到的Music对象的副本
            .FirstOrDefault();          // 获取查询结果中的第一个元素（如果有的话），否则返回null
 
        if (res != null) {
            return new Music(++MessageCnt,res);
        } else {
            Console.WriteLine($"未找到名为 '{data}' 的音乐");
            return null;
        }
    }
}

public class MassageFactoryProducer
{
    public AbstractMassageFactory? GetMassageFactory(string type) {
        if (type == "text") {
            return new ContentFactory();
        }else if(type == "music") {
            return new MusicFactory();
        }else if(type == "pic") {
            return new PictureFactory();
        }else {
            return null;
        }
    }
}
